package com.haiyou.dc.es.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.dc.es.entity.EsHouseTag;
import com.haiyou.dc.es.service.EsHouseTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

/**
 * @author root
 * @date 2020-01-20 16:11:34
 */
@RestController
@RequestMapping("/esHouseTag")
@Slf4j
@Api(tags = "房屋标签映射关系表")
public class EsHouseTagController {
    @Autowired
    private EsHouseTagService esHouseTagService;

    @ApiOperation(value = "保存数据")
    @PutMapping("/save")
    public R save(@RequestBody EsHouseTag esHouseTag) {
        esHouseTagService.insertSelective(esHouseTag);
        return R.ok();
    }

    @ApiOperation(value = "查看数据")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        EsHouseTag esHouseTag = esHouseTagService.selectByPrimaryKey(id);
        return R.ok(esHouseTag);
    }

    @ApiOperation(value = "修改数据")
    @PostMapping("/update")
    public R update(@RequestBody EsHouseTag esHouseTag) {
        esHouseTagService.updateByPrimaryKeySelective(esHouseTag);
        return R.ok();
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        esHouseTagService.deleteByPrimaryKey(id);
        return R.ok();
    }

    @ApiOperation(value = "查看列表数据")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Example example = new Example(EsHouseTag.class);
        //默认排序：id倒序排序
        example.orderBy("id").desc();
        /**
         * 参数说明：
         * count=true,查询总数
         * reasonable=false,总数为10，查看第2页每页10条返回空，如果为true则页数>2每页10条都返回最后一页数据
         * pageSizeZero=true，当pageSize=0时候，返回全部数据，如果为false，返回空数据。
         * 建议使用：count=true，reasonable=false。pageSizeZero=false
         */
        Page<EsHouseTag> page = PageHelper.startPage(pageNum, pageSize, true, false, false);
        esHouseTagService.selectByExample(example);
        PageInfo<EsHouseTag> info = new PageInfo<>(page.getResult());
        return R.ok(info);
    }

}